<?php
/**
 * Set of functions to gather objects by custom field
 * Output in XHTML-compliant <object> element
 *
 * Only handles external videos (i.e. YouTube) by custom field now
 *
 * @copyright 2008 - 2009
 * @version 0.2
 * @author Justin Tadlock
 * @link http://justintadlock.com
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package GetTheObject
 */

/**
 * Catchall function for getting videos
 *
 * @since 0.2
 * @param array $args
 * @return string
 */
function get_the_object( $args = array() ) {

	$defaults = array(
		'custom_key' => array( 'Video', 'video' ),
		'post_id' => false, // Build functionality in later
		'attachment' => false,
		'default_object' => false,
		'src' => false,
		'file' => false,
		'mime_type' => false,
		'class' => 'video',
		'width' => '300',
		'height' => '250',
		'echo' => true
	);

	$args = apply_filters( 'get_the_video_args', $args );

	$args = wp_parse_args( $args, $defaults );
	extract( $args );

	if ( !is_array( $custom_key ) ) :
		$custom_key = str_replace( ' ', '', $custom_key );
		$custom_key = str_replace( array( '+' ), ',', $custom_key );
		$custom_key = explode( ',', $custom_key );
		$args['custom_key'] = $custom_key;
	endif;

	// if($src) $video = object_by_file( $args );

	if ( !$video && $custom_key )
		$video = object_by_custom_field( $args );

	// if(!$video) $video = object_by_attachment($args);

	// if(!video && $default_video) $video = object_by_default($args);

	if ( !$video )
		return false;

	$video = display_the_object( $args, $video );

	if ( $echo )
		echo $video;
	else
		return $video;
}

/**
 * Attempt to get a video by custom field
 *
 * @since 0.1
 * @param array $args
 * @return string
 */
function object_by_custom_field( $args = array() ) {
	global $post;

	extract( $args );

	if ( isset( $custom_key) ) :
		foreach ( $custom_key as $custom ) :
			$video = get_post_meta( $post->ID, $custom, true );
			if ( $video )
				break;
		endforeach;
	endif;

	return $video;
}

/**
 * Function for displaying a video
 *
 * @since 0.1
 * @param array $args
 * @param array $video_arr
 * @return string
 */
function display_the_object( $args, $video ) {

	extract( $args );

	if ( !$mime_type )
		$mime_type = get_the_object_mime_type( $video );
	if ( !$mime_type )
		$mime_type = 'application/x-shockwave-flash';

	if ( $video ) :
		$player = '<object type="' . $mime_type . '" data="' . $video . '" class="' . $class . '" width="' . $width . '" height="' . $height . '">';
		$player .= '<param name="movie" value="' . $video . '" />';
		$player .= '<param name="allowfullscreen" value="true" />';
		$player .= '<param name="wmode" value="transparent" />';
		$player .= '</object>';
	endif;

	return $player;
}

/**
 * Uses the video file extension to determine the mime type
 * Needs to support more extensions
 *
 * @since 0.2
 * @param string $video
 * @return string
 */
function get_the_object_mime_type( $file ) {

	if ( preg_match( '/\.flv$/', $file ) )
		$mime_type = 'application/x-shockwave-flash';

	elseif ( preg_match( '/\.mov$/', $file ) )
		$mime_type = 'video/quicktime';

	elseif ( preg_match( '/\.mov$/', $file ) )
		$mime_type = 'video/quicktime';

	elseif ( preg_match( '/\.asf$/', $file ) || preg_match( '/\.wmv$/', $file ) )
		$mime_type = 'video/x-ms-wmv';

	return $mime_type;
}

/**
 * Function for finding and loading videos
 * This function has been replaced by get_the_object()
 *
 * @deprecated 0.2
 * @since 0.1
 * @param array $args
 */
function get_the_video( $args = array() ) {
	get_the_object( $args );
}

/**
 * Finds a video URL by custom field key
 * This function has been replaced by object_by_custom_field()
 *
 * @deprecated 0.2
 * @since 0.1
 * @param array $args
 */
function video_by_custom_field( $args = array() ) {
	object_by_custom_field( $args );
}

/**
 * Function for displaying a video
 * This function has been replaced by display_the_object()
 *
 * @deprecated 0.2
 * @since 0.1
 * @param array $args
 * @param string $video
 */
function display_the_video( $args, $video ) {
	display_the_object( $args, $video );
}

?>